'''
max x+y+2z
s.t x+2y+3z<=4
    x+y>=1
'''

# 完整的测试代码
from gurobipy import *

try:
    m = Model('mip')

    # 添加变量，GRB.BINARY表示0-1变量
    x = m.addVar(vtype=GRB.BINARY, name='x')
    y = m.addVar(vtype=GRB.BINARY, name='y')
    z = m.addVar(vtype=GRB.BINARY, name='z')

    # 目标函数，GRB.MAXIMIZE表示最大化
    m.setObjective(x + y + 2 * z, GRB.MAXIMIZE)

    # 约束条件
    m.addConstr(x + 2 * y + 3 * z <= 4, 'c0')
    m.addConstr(x + y >= 1, 'c1')

    # 优化器
    m.optimize()

    # 输出变量名，取值以及目标函数值
    for v in m.getVars():
        print(f'变量名：{v.varName},取值：{v.x}')
    print(f'目标值：{m.objval}')
except GurobiError as e:
    print('error code' + str(e.errno) + ':' + str(e))
except AttributeError:
    print('encountered an attribute error')